{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "5b40dde3-2174-41af-b763-0c8db5a96e7b",
   "metadata": {},
   "source": [
    "###Numpy的随机模块"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 54,
   "id": "00332729-1891-445c-ae87-25ef41a220c5",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[0.0722848 , 0.87097054],\n",
       "       [0.64214263, 0.30611482],\n",
       "       [0.17703107, 0.34246047]])"
      ]
     },
     "execution_count": 54,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import numpy as np\n",
    "# 所有的值都是从0到1\n",
    "np.random.rand(3,2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 56,
   "id": "371ac873-1907-4d78-8870-6a929bb66295",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[2, 7, 0, 6],\n",
       "       [0, 3, 0, 2],\n",
       "       [5, 6, 4, 7],\n",
       "       [5, 4, 7, 6],\n",
       "       [0, 9, 7, 3]])"
      ]
     },
     "execution_count": 56,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 返回的都是随机的整数，左闭右开\n",
    "np.random.randint(10,size = (5,4))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 58,
   "id": "59ef1474-9dbb-4d31-a04a-0ca7b7aeac1a",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.4632226712954044"
      ]
     },
     "execution_count": 58,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.random.rand() ### 随机数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 60,
   "id": "f4be6e46-98e3-4b6a-934b-f51985c8e5e8",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.0033760461530232266"
      ]
     },
     "execution_count": 60,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.random.random_sample()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 62,
   "id": "26e71bcd-c186-42ca-b88b-7d02e7055d17",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([4, 6, 0])"
      ]
     },
     "execution_count": 62,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.random.randint(0,10,3) ###生成随机整数数组"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 100,
   "id": "14965a5e-a067-44c7-a571-5b15dd21f2ab",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([ 0.07,  0.07, -0.16,  0.09,  0.08,  0.04,  0.01,  0.  , -0.06,\n",
       "       -0.12])"
      ]
     },
     "execution_count": 100,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "mu,sigma = 0,0.1\n",
    "np.random.normal(mu,sigma,10) ###用于生成正态分布（高斯分布）随机数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 68,
   "id": "20b3d4bb-8d81-4b23-9fbc-8285194fdc35",
   "metadata": {},
   "outputs": [],
   "source": [
    "np.set_printoptions(precision = 2)  ### 用于控制数组显示精度的函数，会将浮点数输出保留2位小数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 92,
   "id": "8fb26511-86f1-4383-984b-77491185aac8",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([-0.05, -0.07,  0.  , -0.06,  0.13, -0.17, -0.1 ,  0.04, -0.16,\n",
       "        0.15])"
      ]
     },
     "execution_count": 92,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "mu,sigma = 0, 0.1\n",
    "np.random.normal(mu,sigma,10)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ad47a975-4cb6-458a-a0fb-02cb506f750c",
   "metadata": {},
   "source": [
    "### 洗牌"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 73,
   "id": "023640ed-4456-4a73-aadb-938574c1d05a",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])"
      ]
     },
     "execution_count": 73,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "liao_array = np.arange(10)\n",
    "liao_array"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 75,
   "id": "d31618fa-6e9d-4d91-8edf-221cb3b52bb8",
   "metadata": {},
   "outputs": [],
   "source": [
    "np.random.shuffle(liao_array)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 77,
   "id": "234dfbbd-6c81-461b-9c93-483b6cd86531",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([3, 5, 7, 6, 0, 2, 4, 8, 9, 1])"
      ]
     },
     "execution_count": 77,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "liao_array"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f0dea623-c46e-466d-bf34-c68f66bc3440",
   "metadata": {},
   "source": [
    "### 随机的种子"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 82,
   "id": "df9cf64d-fc5c-4a1a-a17b-de1ceeacc822",
   "metadata": {},
   "outputs": [],
   "source": [
    "np.random.seed(100) ###设置随机数生成种子的操作"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 104,
   "id": "c842c0a4-1517-4c7a-99b7-a93e2661a113",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([-0.17, -0.11, -0.3 ,  0.  , -0.02, -0.05,  0.01,  0.  ,  0.03,\n",
       "       -0.08])"
      ]
     },
     "execution_count": 104,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "mu, sigma = 0,0.1\n",
    "np.random.normal(mu,sigma,10) "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "2727950d-2a5c-4620-b10e-c6d51a113d2e",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
